package com.example.springbootsfzy.controller.sunnan;

import com.example.springbootsfzy.domain.QaReplies;
import com.example.springbootsfzy.service.sunnan.QaRepliesService;
import com.example.springbootsfzy.util.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/qaReplies")
public class QaRepliesController {

    @Autowired
    private QaRepliesService qaRepliesService;

    /**
     * 查询所有答疑回复
     * @param pageNum 当前页码
     * @param pageSize 每页显示的数量
     * @return PageInfo<QaReplies>
     */
    @GetMapping
    @ResponseBody
    public PageInfo<QaReplies> getAllQaReplies(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize){
        return qaRepliesService.getQaReplies(pageNum, pageSize);
    }
    /**
     * 根据问题id获取回复
     */
    @GetMapping("/{questionId}")
    @ResponseBody
    public PageInfo<QaReplies> getQaRepliesById(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize,
            @PathVariable int questionId){
        return qaRepliesService.getQaRepliesById(pageNum,pageSize,questionId);
    }
    /**
     * 根据会员id查找回复
     */
    @GetMapping("/member/{memberId}")
    @ResponseBody
    public PageInfo<QaReplies> getRepliesByMemberId(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize,
            @PathVariable int memberId){
        return qaRepliesService.getRepliesByMemberId(pageNum, pageSize, memberId);
    }



    /**
     * 添加答疑回复
     * @param qaReplies 答疑回复
     * @return R
     */
    @PostMapping
    @ResponseBody
    public R addQaReplies(@RequestBody QaReplies qaReplies){
        if (qaRepliesService.addQaReplies(qaReplies) > 0){
            return R.ok(null);
        }else {
            return R.error("添加失败");
        }
    }

    /**
     * 删除答疑回复
     * @param id 答疑回复id
     */
    @DeleteMapping("/{id}")
    @ResponseBody
    public R deleteQaReplies(@PathVariable int id){
        if (qaRepliesService.deleteQaReplies(id) > 0){
            return R.ok(null);
        }else {
            return R.error("删除失败");
        }
    }

    /**
     * 修改答疑回复
     * @param qaReplies 答疑回复
     */
    @PutMapping
    @ResponseBody
    public R updateQaReplies(@RequestBody QaReplies qaReplies){
        if (qaRepliesService.updateQaReplies(qaReplies) > 0){
            return R.ok(null);
        }else {
            return R.error("修改失败");
        }
    }
}
